package com.lmaos.allcontrol.beancontrol.entity_;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.HashMap;

import com.lmaos.allcontrol.beancontrol.class_.MyClassLibrary;
import com.lmaos.allcontrol.beancontrol.class_.MyClassLibrary.ClassLoaderNotFoundException;
import com.lmaos.allcontrol.beancontrol.method_.MethodUtils;
import com.lmaos.allcontrol.beancontrol.utils_.InjectorParams;
import com.lmaos.allcontrol.beancontrol.utils_.ObjUtils;
/** class对应的值<br>
 * 
 * LMClassParam(InjectorParams injectorParams) 自定义参数注入
 * @author lmaos 
 * 2016年11月15日 下午10:34:42
 */
public class LMClassParam extends HashMap<Class, Object>{
	private static final long serialVersionUID = 1L;
	
	private InjectorParams injectorParams;
	private LMValueParam valueParam = new LMValueParam(this);
	private MyClassLibrary classLibrary = MyClassLibrary.getClassLibrary();
	private Object[] defalutValue = new Object[0];
	private Object defaultBean = null;
	public LMClassParam() {
		put(this);
		put(classLibrary);
	}
	
	public LMClassParam(InjectorParams injectorParams) {
		this();
		setInjectorParams(injectorParams);
	}
	
	
	public LMClassParam(InjectorParams injectorParams,
			MyClassLibrary classLibrary) {
		super();
		this.injectorParams = injectorParams;
		this.classLibrary = classLibrary;
		put(this);
		put(classLibrary);
		setInjectorParams(injectorParams);
	}

	public InjectorParams getInjectorParams() {
		return injectorParams;
	}

	public LMClassParam put(Object obj){
		put(obj.getClass(), obj);
		return this;
	}
	
	public void setInjectorParams(InjectorParams injectorParams) {
		if(injectorParams!=null){
			this.injectorParams = injectorParams;
			put(injectorParams);
		}
	}
	
	public LMValueParam getLMValueParam(){
		return valueParam;
	}
	
	public Object[] toObjects(AccessibleObject accessibleObject,Class[] types){
		if(injectorParams == null){
			return ObjUtils.getParamsValue(types, this);
		}else{
			return injectorParams.getParamsValue(accessibleObject, types, this);
		}
	}
	
	public Object executeMethod(Object obj,Method method) throws Exception{
		Object[] values = this.toObjects(method, method.getParameterTypes());
		return MethodUtils.executeMethod(obj, method, values);
	}
	public void setClassLibrary(MyClassLibrary classLibrary) {
		this.classLibrary = classLibrary;
		put(classLibrary);
	}
	public Class loadClass(String name,String className) throws ClassNotFoundException, ClassLoaderNotFoundException{
		return classLibrary.loadClass(name,className);
	}
	public ClassLoader getClassLoader(String name){
		return classLibrary.getClassLoader(name);
	}
	public LMClassParam setDefalutValue(Object[] defalutValue) {
		this.defalutValue = defalutValue;
		return this;
	}
	public Object[] getDefalutValue() {
		return defalutValue;
	}
	public LMClassParam setDefaultBean(Object defaultBean) {
		this.defaultBean = defaultBean;
		return this;
	}
	@Override
	public Object get(Object key) {
		// TODO Auto-generated method stub
		return super.get(key);
	}

}
